/* **********************************************************************
版本: 2021.1.25f1c1
作者: wzz
日期: 2022-4-8
描述: 重绘FastScrollView 在Inspector上的显示，使其方便使用
*************************************************************************/

using UnityEditor;
using UnityEditor.AnimatedValues;

[CustomEditor(typeof(FastScrollView.FastScrollView), true)]
[CanEditMultipleObjects]
public class FastScrollViewEditor : Editor
{
    private SerializedProperty mItemPrefabDataList;
    private SerializedProperty scrollType;
    private SerializedProperty paddingFit;
    private SerializedProperty firstPadding;
    private SerializedProperty lastPadding;
    private SerializedProperty scrollExceedDistance;
    private SerializedProperty isSnap;
    private SerializedProperty moveToEaseMode;
    private SerializedProperty isSnapViewCenter;
    private SerializedProperty stopSnapVelocity;
    private SerializedProperty snapNextItemDistancePer;
    private SerializedProperty scrollEaseMode;
    private SerializedProperty snapDuration;
    private SerializedProperty isGrid;
    private SerializedProperty fitOneColOrRowItemCount;
    private SerializedProperty oneColOrRowItemCount;
    private SerializedProperty gridAlignment;
    private SerializedProperty gridPadding;
    private SerializedProperty gridSpacing;
    private SerializedProperty fitGridPaddingLR;
    private SerializedProperty fitGridPaddingTB;
    private SerializedProperty fitGridSpacing;
    private SerializedProperty itemNotFixSize;

    private AnimBool show_paddingFit;
    private AnimBool show_isSnap;
    private AnimBool show_isGrid;

    protected virtual void OnEnable()
    {
        mItemPrefabDataList = serializedObject.FindProperty("mItemPrefabDataList");
        scrollType = serializedObject.FindProperty("scrollType");
        paddingFit = serializedObject.FindProperty("paddingFit");
        firstPadding = serializedObject.FindProperty("firstPadding");
        lastPadding = serializedObject.FindProperty("lastPadding");
        scrollExceedDistance = serializedObject.FindProperty("scrollExceedDistance");
        moveToEaseMode = serializedObject.FindProperty("moveToEaseMode");
        isSnap = serializedObject.FindProperty("isSnap");
        isSnapViewCenter = serializedObject.FindProperty("isSnapViewCenter");
        stopSnapVelocity = serializedObject.FindProperty("stopSnapVelocity");
        snapNextItemDistancePer = serializedObject.FindProperty("snapNextItemDistancePer");
        scrollEaseMode = serializedObject.FindProperty("scrollEaseMode");
        snapDuration = serializedObject.FindProperty("snapDuration");
        isGrid = serializedObject.FindProperty("isGrid");
        fitOneColOrRowItemCount = serializedObject.FindProperty("fitOneColOrRowItemCount");
        oneColOrRowItemCount = serializedObject.FindProperty("oneColOrRowItemCount");
        gridAlignment = serializedObject.FindProperty("gridAlignment");
        gridPadding = serializedObject.FindProperty("gridPadding");
        gridSpacing = serializedObject.FindProperty("gridSpacing");
        fitGridPaddingLR = serializedObject.FindProperty("fitGridPaddingLR");
        fitGridPaddingTB = serializedObject.FindProperty("fitGridPaddingTB");
        fitGridSpacing = serializedObject.FindProperty("fitGridSpacing");
        itemNotFixSize = serializedObject.FindProperty("itemNotFixSize");

        show_paddingFit = new AnimBool(Repaint);
        show_isSnap = new AnimBool(Repaint);
        show_isGrid = new AnimBool(Repaint);
        SetAnimBools(true);
    }

    public override void OnInspectorGUI()
    {
        SetAnimBools(false);
        serializedObject.Update();

        EditorGUILayout.LabelField("鼠标放在属性名上，可查看说明");

        EditorGUILayout.PropertyField(mItemPrefabDataList);
        EditorGUILayout.Space();
        EditorGUILayout.PropertyField(scrollType);
        EditorGUILayout.PropertyField(paddingFit);

        if (EditorGUILayout.BeginFadeGroup(show_paddingFit.faded))
        {
            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(firstPadding);
            EditorGUILayout.PropertyField(lastPadding);
            EditorGUI.indentLevel--;
            EditorGUILayout.Space();
        }
        EditorGUILayout.EndFadeGroup();

        EditorGUILayout.PropertyField(scrollExceedDistance);
        EditorGUILayout.PropertyField(moveToEaseMode);
        EditorGUILayout.PropertyField(isSnap);

        if (EditorGUILayout.BeginFadeGroup(show_isSnap.faded))
        {
            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(isSnapViewCenter);
            EditorGUILayout.PropertyField(stopSnapVelocity);
            EditorGUILayout.PropertyField(snapNextItemDistancePer);
            EditorGUILayout.PropertyField(scrollEaseMode);
            EditorGUILayout.PropertyField(snapDuration);
            EditorGUI.indentLevel--;
            EditorGUILayout.Space();
        }
        EditorGUILayout.EndFadeGroup();

        if (!isGrid.boolValue)
            EditorGUILayout.PropertyField(itemNotFixSize);

        if (!itemNotFixSize.boolValue)
            EditorGUILayout.PropertyField(isGrid);
        if (EditorGUILayout.BeginFadeGroup(show_isGrid.faded))
        {
            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(fitOneColOrRowItemCount);
            if (!fitOneColOrRowItemCount.boolValue)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(oneColOrRowItemCount);

                EditorGUI.indentLevel--;
            }
            else
            {
                EditorGUILayout.PropertyField(fitGridPaddingLR);
                EditorGUILayout.PropertyField(fitGridPaddingTB);
                EditorGUILayout.PropertyField(fitGridSpacing);
            }
            EditorGUILayout.PropertyField(gridAlignment);
            EditorGUILayout.PropertyField(gridPadding);
            EditorGUILayout.PropertyField(gridSpacing);

            EditorGUI.indentLevel--;
            EditorGUILayout.Space();
        }
        EditorGUILayout.EndFadeGroup();

        EditorGUILayout.Space();

        serializedObject.ApplyModifiedProperties();
    }

    private void SetAnimBools(bool instant)
    {
        SetAnimBool(show_paddingFit, !paddingFit.hasMultipleDifferentValues && !paddingFit.boolValue, instant);

        SetAnimBool(show_isSnap, !isSnap.hasMultipleDifferentValues && isSnap.boolValue, instant);
        SetAnimBool(show_isGrid, !isGrid.hasMultipleDifferentValues && isGrid.boolValue, instant);
    }

    private void SetAnimBool(AnimBool a, bool value, bool instant)
    {
        if (instant)
            a.value = value;
        else
            a.target = value;
    }
}